package com.xju.atpgs.controller;

import com.xju.atpgs.entity.*;
import com.xju.atpgs.service.DictionaryService;
import com.xju.atpgs.service.QuestionService;
import com.xju.atpgs.service.SectionService;
import com.xju.atpgs.service.UserService;
import com.xju.atpgs.utils.FileUtil;
import com.xju.atpgs.utils.IDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebParam;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/question")
public class QuestionController {

    private Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private QuestionService questionService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private SectionService sectionService;
    @Autowired
    private UserService userService;

    @RequestMapping("/upload")
    public String questionUpload(Model model){
        List<Dictionary> types=dictionaryService.searchByType("试题类型");
        List<Section> sections=sectionService.findAll();
        List<Dictionary> levels=dictionaryService.searchByType("试题难度");
        model.addAttribute("types",types);
        model.addAttribute("sections",sections);
        model.addAttribute("levels",levels);
        return "module_two/question_upload";
    }

    @RequestMapping("/single_upload")
    @ResponseBody
    public Object singleUpload(String section,String type,String level,String content,String answer,String describe ){
        Question question=new Question();
        question.setQuesId(IDGenerator.getId());
        question.setQuesAnswer(answer);
        question.setQuesContent(content);
        question.setQuesDescribe(describe);
        question.setQuesSection(section);
        question.setQuesType(type);
        Random random=new Random();
        Double newLevel;
        switch (level){
            case "易":
                newLevel=random.nextDouble()*0.33+0.66;
                question.setQuesLevel(String.valueOf(newLevel));break;
            case "中等":
                newLevel=random.nextDouble()*0.33+0.33;
                question.setQuesLevel(String.valueOf(newLevel));break;
            case "难":
                newLevel=random.nextDouble()*0.33;
                question.setQuesLevel(String.valueOf(newLevel));break;

        }
        question.setQuesStatus(dictionaryService.findByValue("待审核").getDicId());
        question.setQuesCreator(userService.findByName("谢世科").getUserId());
        questionService.insert(question);
        Map<String,Object> map=new HashMap<>();
        map.put("success",true);
        return map;
    }

    @RequestMapping("/file_upload")
    @ResponseBody
    public Object fileUpload(@RequestParam("file")MultipartFile file){
        if (file != null) {
            try {
                FileUtil.uploadFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        questionService.uploadFile(file);
        Map<String,Object> map=new HashMap<>();
        map.put("success",true);
        return map;
    }

    @RequestMapping("/manage")
    public String manage(Model model,String section,String type,String level){
        ResultQuestionManage resultQuestionManage=questionService.manage(section,type,level);
        model.addAttribute("resultQuestionManage",resultQuestionManage);
        return "module_two/question_manager";
    }


    @RequestMapping("/modify-request")
    public String modifyRequest(String Id, Model model){
        Question question=questionService.modifyRequest(Id);
        List<Dictionary> types=dictionaryService.searchByType("试题类型");
        List<Section> sections=sectionService.findAll();
        List<Dictionary> levels=dictionaryService.searchByType("试题难度");
        model.addAttribute("types",types);
        model.addAttribute("sections",sections);
        model.addAttribute("levels",levels);
        model.addAttribute("question",question);
        return "module_two/question_modify";
    }
    @RequestMapping("/modify-detail")
    public String questionDetail(String Id,String section,String type,String level, Model model){
        ResultQuestion question=questionService.questionDetail(Id,section,type,level);
        model.addAttribute("question",question);
        return "module_two/question_detail";
    }
    @RequestMapping("/page-detail")
    public String pageDetail(String Id,String section,String type,String level, String page,Model model){
        ResultQuestion question=questionService.pageDetail(Id,section,type,level,page);
        model.addAttribute("question",question);
        return "module_two/question_detail";
    }


}
